from django import forms
from .models import UserAsk
import re


class UserAskForm(forms.ModelForm):
    class Meta:
        model = UserAsk
        # 用谁↓
        # fields=["name","course","phone"]
        # 如果是所有字段↓
        # fields = "__all__"
        # 除了谁↓
        exclude = ["add_time"]

    #     clean_字段名   做验证
    def clean_phone(self):
        phone = self.cleaned_data["phone"]
        com = re.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$")
        # 匹配
        if com.match(phone):
            return phone
        else:
            raise forms.ValidationError("手机号码不合法")


class UserCommentForm(forms.Form):
    course = forms.IntegerField(required=True)
    content = forms.CharField(required=True, min_length=1, max_length=300)
